package com.mzj.saas.rent;

import com.haixiaoke.saas.repository.core.AjaxResult;
import com.haixiaoke.saas.repository.core.BaseController;
import com.haixiaoke.saas.repository.core.page.TableDataInfo;
import com.haixiaoke.saas.repository.pojo.OpHouse;
import com.haixiaoke.saas.repository.service.IOpHouseService;
import com.mzj.saas.constant.OpConstant;
import com.mzj.saas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公众号房源Controller
 *
 * @author qixi
 * @date 2024-01-16
 */
@RestController
@RequestMapping("/opHouse")
public class OpHouseController extends BaseController {

    @Autowired
    private IOpHouseService opHouseService;
    @Autowired
    private LoginUserUtil loginUserUtil;

    /**
     * 查询公众号房源列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OpHouse opHouse) {
        startPage();
        Integer loginType = loginUserUtil.getLoginType();
        if (loginType == 1) {
            opHouse.setOrgId(loginUserUtil.getOrgId());
        }
        List<OpHouse> list = opHouseService.selectOpHouseList(opHouse);
        return getDataTable(list);
    }

    /**
     * 获取公众号房源详细信息
     */
    // @PreAuthorize("@ss.hasPermi('business:house:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(opHouseService.selectOpHouseById(id));
    }

    /**
     * 新增公众号房源
     */
    @PostMapping
    public AjaxResult add(@RequestBody OpHouse opHouse) {
        Long orgId = loginUserUtil.getOrgId();
        opHouse.setOrgId(orgId);
        return toAjax(opHouseService.insertOpHouse(opHouse));
    }

    /**
     * 修改公众号房源
     */
    @PutMapping
    public AjaxResult edit(@RequestBody OpHouse opHouse) {
        return toAjax(opHouseService.updateOpHouse(opHouse));
    }

    /**
     * 删除公众号房源
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(opHouseService.deleteOpHouseByIds(ids));
    }

    /**
     * 上架公众号房源
     */
    @PutMapping("/publish/{id}")
    public AjaxResult publish(@PathVariable Long id) {
        return toAjax(opHouseService.updateOpHouseStatus(id, OpConstant.HOUSE_STATUS_ON));
    }

    /**
     * 下架公众号房源
     */
    @PutMapping("/unpublish/{id}")
    public AjaxResult unpublish(@PathVariable Long id) {
        return toAjax(opHouseService.updateOpHouseStatus(id, OpConstant.HOUSE_STATUS_OFF));
    }

    /**
     * 批量上架公众号房源
     */
    @PutMapping("/publishBatch/{ids}")
    public AjaxResult publishBatch(@PathVariable Long[] ids) {
        return toAjax(opHouseService.updateOpHouseStatusBatch(ids, OpConstant.HOUSE_STATUS_ON));
    }

    /**
     * 批量下架公众号房源
     */
    @PutMapping("/unpublishBatch/{ids}")
    public AjaxResult unpublishBatch(@PathVariable Long[] ids) {
        return toAjax(opHouseService.updateOpHouseStatusBatch(ids, OpConstant.HOUSE_STATUS_OFF));
    }
}
